var SettingHandler=function () {

}

SettingHandler.prototype.listenUploadAvatarEvent=function () {
    $("#avatar-input").on("change",function () {
        var avatar=this.files[0];
        var formData=new FormData();
        formData.append('avatar',avatar);
        zlajax.post({
            url:'/upload/avatar/',
            data:formData,
            processData: false,
            contentType:false,
            success:function (result) {
                if(result['code']==200){
                    var avatar=result['data']['avatar'];
                    var avatar_url='/media/avatar/'+avatar;
                    $("#avatar-img").attr("src",avatar_url);
                }else{
                    console.log(result['message']);
                }
            }
        })
    })
}

SettingHandler.prototype.listenEditProfileEvent=function () {
    $("#submit-btn").on("click",function (event) {
        event.preventDefault();
        var signature=$("#signagure-input").val();
        if(!signature){
            alert("保存成功!")
        }else if(signature && (signature.length<2 || signature.length>50)){
            alert("个性签名长度必须在2-50之间!")
        }else{
            zlajax.post({
                url:'/setting/profile/',
                data:{
                    signature
                },
                success:function (result) {
                    if(result['code']==200){
                        alert("保存成功");
                    }else{
                        alert(result['message']);
                    }

                }
            })
        }

    })
}

SettingHandler.prototype.run=function () {
    this.listenUploadAvatarEvent();
    this.listenEditProfileEvent();
}

$(function () {
    var handler=new SettingHandler();
    handler.run();
})